import http from './http.js'
import config from '../config/index.js'

export default {
    // 打开认证页面
    getCode(data) {
        const url = `https://gitee.com/oauth/authorize?client_id=${config.client_id}&redirect_uri=${data.redirect_uri}&response_type=code`
        window.open(url)
    },
    // 获取用户的所有公开仓库
    getPubRepos(data) {
        const url = `https://gitee.com/api/v5/users/${data.username}/repos`
        return http.get(url, {
            params: {access_token: data.access_token}
        }).then(res => {
            return Promise.resolve(res)
        })
    },
    // 获取所有分支
    getAllBranch(data) {
        const url = `/api/v5/repos/${data.owner}/${data.repo}/branches`
        return http.get(url, {
            params: {access_token: data.access_token}
        }).then(res => {
            return Promise.resolve(res)
        })
    },

    // 获取仓库某路径下的内容
    getAllFile(data) {
        const url = `/api/v5/repos/${data.owner}/${data.repo}/contents${data.path?data.path:''}`
        return http.get(url, {
            params: {access_token: data.access_token}
        }).then(res => {
            return Promise.resolve(res)
        })
    },
    // 获取某个文件内容
    getFileContent(data) {
        const url = `https://gitee.com/api/v5/repos/${data.owner}/${data.repo}/git/blobs/${data.sha}`
        return http.get(url, {
            params: {access_token: data.access_token}
        }).then(res => {
            let tmp = atob(res.content)
            localStorage.setItem("userInfo", tmp)
            return Promise.resolve(JSON.parse(tmp))
        })
    },
    // 查看token 无效
    // getToken() {
    //     return http.get('https://gitee.com/dusw/file-database-1/raw/master/token.txt').then(res => {
    //         return Promise.resolve(res)
    //     })
    // }
}